package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.entity.Coach;
import com.lovelyj.trip_by_train.entity.DailyTrain;
import com.lovelyj.trip_by_train.entity.Seat;
import com.lovelyj.trip_by_train.entity.Station;
import com.lovelyj.trip_by_train.service.CoachService;
import com.lovelyj.trip_by_train.service.DailyTrainService;
import com.lovelyj.trip_by_train.service.SeatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/coach")
@RestController
public class CoachController {

    @Autowired
    private CoachService coachService;

    @Autowired
    private SeatService seatService;

    @Autowired
    private DailyTrainService dailyTrainService;

    private boolean ifInDaily(String trainNo) {
        LambdaQueryWrapper<DailyTrain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrain::getTrainNum, trainNo);
        return dailyTrainService.count(queryWrapper) > 0;
    }



    @GetMapping("/page")
    public R<Page<Coach>> selectPage(HttpServletRequest request, Integer page, String trainNum, String coachNum){
        log.info("selectPage..."+page);
        Page<Coach> pageInfo=new Page<>(page,10);
        LambdaQueryWrapper<Coach> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(trainNum!=null,Coach::getTrainNum,trainNum);
        queryWrapper.like(coachNum!=null,Coach::getCoachNum,coachNum);
        queryWrapper.orderByAsc(Coach::getTrainNum);
        queryWrapper.orderByAsc(Coach::getCoachNum);
        coachService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping("/create")
    public R<String> create(HttpServletRequest request, @RequestBody Coach coach) {
        log.info("开始创建");
        if(ifInDaily(coach.getTrainNum())){
            return R.error("该车次在daily中！不可修改");
        }

        LambdaQueryWrapper<Coach> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(coach.getTrainNum()!=null,Coach::getTrainNum,coach.getTrainNum());
        lambdaQueryWrapper.eq(coach.getCoachNum()!=null,Coach::getCoachNum,coach.getCoachNum());
        if(coachService.count(lambdaQueryWrapper) > 0) {
            return R.error("该车厢已创建");
        }
        coach.setSeatNumber(coach.getSeatColNumber()*coach.getSeatRowNumber());

        coachService.saveWithSeats(coach);
        return R.success("创建成功");
    }

    @PostMapping("/deleteByIds")
    public R<String> deleteByIds(HttpServletRequest request,@RequestBody List<Long> selectedIds){

        log.info("deleteByIds...");
        coachService.removeByIds(selectedIds);

        return R.success("删除成功");
    }

    @DeleteMapping
    public R<String> deleteById(HttpServletRequest request,Long id){

        log.info("deleteById..."+id);
        coachService.removeById(id);
        return R.success("删除成功");
    }


    @PutMapping("/update")
    public R<String> update(HttpServletRequest request,@RequestBody Coach coach) {
        log.info("update...");

        String trainNum = coachService.getById(coach.getId()).getTrainNum();

        if(ifInDaily(trainNum)){
            return R.error("原车次在daily中！不可修改");
        }

        if(ifInDaily(coach.getTrainNum())){
            return R.error("新车次在daily中！不可修改");
        }
        coachService.updateById(coach);
        return R.success("修改成功");
    }

}
